import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import userLogin from '@/page/login/userLogin'
import Layout from '@/page/index/layout'
const Chart = () => import('@/page/index/chart.vue')
const Menu = () => import('@/page/menu/index')
const Menu1 = () => import('@/page/menu/index1')
const Role = () => import('@/page/role/index')
const Role1 = () => import('@/page/role/index1')
const Admin = () => import('@/page/admin/index')
const Admin1 = () => import('@/page/admin/index1')
const Cate = () => import('@/page/cate/index')
const Goods = () => import('@/page/goods/index')

Vue.use(Router)

const router = new Router({
  routes: [
    // {
    //   path: '/chart',
    //   name: 'chart',
    //   component: Chart
    // },
    {
      path: '/layout',
      name: 'layout',
      component: Layout,
      children: [
        {
          path: '',
          name: 'chart',
          component: Chart
        },
        {
          path: 'menu',
          component: Menu
        },

        {
          path: 'menu/add',
          component: Menu1
        },
        {
          path: 'menu/:id',
          component: Menu1
        },
        // 角色管理路由
        {
          path: 'role',
          component: Role
        },
        {
          path: 'role/add',
          component: Role1
        },
        {
          path: 'role/:id',
          component: Role1
        },
        // 管理员管理路由
        {
          path: 'admin',
          component: Admin
        },
        {
          path: 'admin/add',
          component: Admin1
        },
        {
          path: 'admin/:uid',
          component: Admin1
        },
        // 商品分类路由
        {
          path: 'cate',
          component: Cate
        },
        // 商品管理
        {
          path: 'goods',
          component: Goods
        },

      ]
    },
    {
      path: '/',
      name: 'HelloWorld',
      component: HelloWorld
    },
    {
      path: '/login',
      name: 'login',
      component: userLogin
    }
  ]
})

router.beforeEach((to, from, next) => {
  document.title = to.meta.title || '后台管理系统'
  // 判断路由
  if (to.path === '/login') {
    // 登录页不做拦截
    next()
  } else {
    // 不去登录页，需要判断是否登录
    if (localStorage.getItem('userInfo')) {
      // localStorage有数据，说明已经登录
      next()
    } else {
      // localStorage没有数据，说明没有登录，就去登陆
      next('./login')
    }
  }
})

export default router